﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using He.Common;
using System.Data.SQLite;
using System.Collections.Generic;

namespace He.DAL
{
    /// <summary>
    /// 数据访问类:PicturesExtension
    /// </summary>
    public partial class attr_value
    {
        public readonly string TblName = SysKeys.TABLE_PREFIX + "attr_value";

        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByFieldId(int fieldId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where field_id=@field_id");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@field_id", DbType.Int32,4)
            };
            parameters[0].Value = fieldId;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByAttrItemId(int attrItemId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where content like '%," + attrItemId + ",%'");

            return DbHelperSQLite.Exists(strSql.ToString());
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByPFId(int pictureId, int fieldId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where picture_id=@picture_id and field_id=@field_id ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@picture_id", DbType.Int32,4),
                    new SQLiteParameter("@field_id", DbType.Int32,4)
            };
            parameters[0].Value = pictureId;
            parameters[1].Value = fieldId;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public He.Model.attr_value GetModel(string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + TblName);
            strSql.Append(" where " + strWhere);

            He.Model.attr_value model = new He.Model.attr_value();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["picture_id"] != null && ds.Tables[0].Rows[0]["picture_id"].ToString() != "")
                {
                    model.picture_id = int.Parse(ds.Tables[0].Rows[0]["picture_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["field_id"] != null && ds.Tables[0].Rows[0]["field_id"].ToString() != "")
                {
                    model.field_id = int.Parse(ds.Tables[0].Rows[0]["field_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["field_name"] != null && ds.Tables[0].Rows[0]["field_name"].ToString() != "")
                {
                    model.field_name = ds.Tables[0].Rows[0]["field_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "")
                {
                    model.content = ds.Tables[0].Rows[0]["content"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateByPFId(He.Model.attr_value model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + TblName + " set ");
            strSql.Append("content=@content");
            strSql.Append(" where picture_id=@picture_id and field_id=@field_id ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@content", DbType.String),
                    new SQLiteParameter("@picture_id", DbType.Int32,8),
                    new SQLiteParameter("@field_id", DbType.Int32,8)};
            parameters[0].Value = model.content;
            parameters[1].Value = model.picture_id;
            parameters[2].Value = model.field_id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByPid(int picture_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from he_attr_value ");
            strSql.Append(" where picture_id=@picture_id");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@picture_id", DbType.Int32,4)
            };
            parameters[0].Value = picture_id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByFid(int field_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from he_attr_value ");
            strSql.Append(" where field_id=@field_id");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@field_id", DbType.Int32,4)
            };
            parameters[0].Value = field_id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
		/// 获得产品列表的扩展字段值
		/// </summary>
		public DataSet GetAttrValueListByPictureIds(List<int> idList)
        {
            if (idList != null && idList.Count > 0)
            {
                string ids = string.Join(",", idList);
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat("SELECT a.title AS field_title,b.* FROM he_attr_field a LEFT JOIN he_attr_value b ON a.id=b.field_id WHERE b.picture_id IN({0}) ", ids);
                return DbHelperSQLite.Query(strSql.ToString());
            }
            return null;
        }
        #endregion  Method
    }
}

